import json

import requests
import websocket

import _thread as thread


def wsend(socket,messesge):
    socket.send(messesge)
    r = socket.recv()
    print(type(r))
    while r:
        print(str(r))
        r = socket.recv()

post_headers = {
    'Host':'game.wsmud.com',
    'Connection':'keep-alive',
    'Pragma':'no-cache',
    'Cache-Control':'no-cache',
    'Accept':'application/json, text/javascript, */*; q=0.01',
    'Origin':'http://game.wsmud.com',
    'X-Requested-With':'XMLHttpRequest',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
    'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
    'Referer':'http://game.wsmud.com/',
    'Accept-Language':'zh-CN,zh;q=0.9',
}

date = {
    'code':'yu736019332',
    'pwd':'45278835',
}

post_url = 'http://game.wsmud.com/UserAPI/Login'


s = requests.Session()

s.post(url = post_url, headers = post_headers, data=date)

p = s.cookies['p']
u = s.cookies['u']

get_headers = {
    'Host':'game.wsmud.com',
    'Connection':'keep-alive',
    'Pragma':'no-cache',
    'Cache-Control':'no-cache',
    'Accept':'application/json, text/javascript, */*; q=0.01',
    'X-Requested-With':'XMLHttpRequest',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
    'Referer':'http://game.wsmud.com/',
    'Accept-Language':'zh-CN,zh;q=0.9',
}

get_url = "http://game.wsmud.com/Game/GetServer"
s.headers.update(get_headers)
response = s.get(url = get_url)

server = json.loads(response.text)

ip = server[1]['IP']

port = server[1]["Port"]

w = websocket.create_connection("ws://%s:%s" % (ip,port))

wsend(w,u+" "+p)


exit()

id = 'b8uq265ecd2'

wsend(w,'login'+' '+id)



wsend(w,'jh fam 4 start')



w.close()
